package org.flexlancer.databases
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public class dbSalesInvoice extends EventDispatcher
	{
		private static const TABLE:String = "salesinvoice";
		public function dbSalesInvoice(target:IEventDispatcher=null)
		{
			super(target);
		}
		public static function List(condition:String = ""):Array{
			try{
				return DatabaseManager.Manager.Select(TABLE,null,condition);;
			}catch(err:Error){
				throw err;
			}
			return new Array;
		}
		public static function Add(
				customerReference:Number, 
				salesDate:String, 
				salesNote:String, 
				itemsCount:Number, 
				previousCredit:Number, 
				currentTotal:Number, 
				discount:Number,
				totalPayable:Number,
				amountPaid:Number,
				balance:Number,
				invoiceItems:Array,
				keepBalance:Number = 0):void{

			try{//invoiceid INTEGER PRIMARY KEY AUTOINCREMENT",
			//"refcustomer INTEGER","salesdate TEXT",
			//"salesnote TEXT","itemscount INTEGER",
			//"previouscredit INTEGER","currenttotal INTEGER",
			//"discount INTEGER","totalpayable INTEGER",
			//"amountpaid INTEGER","balance INTEGER"
			
				DatabaseManager.Manager.Begin();
				var Obj:Object = new Object();
				Obj.refcustomer = customerReference;
				Obj.salesdate =  " '" + salesDate + "' ";
				Obj.salesnote = " '" + salesNote + "' ";
				Obj.itemscount = itemsCount;
				Obj.previouscredit = previousCredit;
				Obj.currenttotal = currentTotal;
				
				Obj.discount = discount;
				Obj.totalpayable = totalPayable;
				Obj.amountpaid = amountPaid;
				Obj.balance = balance;
				Obj.keepbalance = keepBalance;
				
				DatabaseManager.Manager.Insert(TABLE, Obj);
				var insertID:Number = DatabaseManager.Manager.LastInsertRowID;
				
				if (invoiceItems){
					dbSalesInvoiceItems.Add(insertID,invoiceItems);
				}

				DatabaseManager.Manager.Commit();
			}catch(err:Error){
				DatabaseManager.Manager.Rollback();
				throw err;
			}
		}
		public static function Delete(condition:String = ""):void{
			try{
				DatabaseManager.Manager.Delete(TABLE,condition);
			}catch(err:Error){
				throw err;
			}
		}
/* 		public static function Update(
				productID:String,
				productTitle:String, 
				barcode:String, 
				description:String, 
				instock:Number, 
				orderlevel:Number, 
				cost:Number, 
				producttypeid:String,
				supplierid:String,
				unitid:String,
				price:Array):void{

			try{//customerid INTEGER PRIMARY KEY AUTOINCREMENT","customername TEXT", "customertype TEXT", "credit REAL"]);
				DatabaseManager.Manager.Begin();
				var custObj:Object = new Object();
				custObj.title = " '" + productTitle + "' ";
				custObj.barcode =  " '" + barcode + "' ";
				custObj.desc = " '" + description + "' ";
				custObj.instock = instock;
				custObj.orderlevel = orderlevel;
				custObj.cost = cost;
				
				custObj.refsupplier = supplierid;
				custObj.refproducttype = producttypeid;
				custObj.refunit = unitid;
				
				DatabaseManager.Manager.Update(TABLE, custObj , "productid = " + productID);
				
				if (price){
					var arrPrices:Array = new Array();
					for each ( var singlePrice:Object in price ){
						//["refid TEXT","contacttype TEXT", "contactdetail TEXT"]
						var contObj:Object = new Object();
						contObj.refid = " 'PROD-"+productID+"' ";
						contObj.title = " '"+singlePrice.title+"' ";
						contObj.price = singlePrice.price;
						contObj.markup = singlePrice.markup;
						arrPrices.push(contObj);
					}
					ProductPrice.Delete("refid LIKE 'PROD-"+productID+"'");
					ProductPrice.Add(arrPrices);
				}

				DatabaseManager.Manager.Commit();
			}catch(err:Error){
				DatabaseManager.Manager.Rollback();
				throw err;
			}
		}
 */	}
}